package cn.iocoder.yudao.module.erp.api;

import cn.iocoder.yudao.module.erp.controller.admin.purchaseorder.OrderApi;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseorder.PurchaseOrderDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.saleorder.SaleOrderDO;
import cn.iocoder.yudao.module.erp.dal.mysql.purchaseorder.PurchaseOrderMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.saleorder.SaleOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class OrderApiImpl implements OrderApi {
    @Resource
    private SaleOrderMapper saleOrderMapper;
    @Resource
    private PurchaseOrderMapper purchaseOrderMapper;

    @Override
    public BigDecimal getSumSaleOrderAmount() {
        List<SaleOrderDO> saleOrderDOS = saleOrderMapper.selectAmountTotal(0);
        return saleOrderDOS.stream().map((value) -> {
            if (value.getTotalAmount() != null) {
                return value.getTotalAmount();
            }
            return BigDecimal.ZERO;
        }).reduce(BigDecimal.ZERO,BigDecimal::add);
    }

    @Override
    public BigDecimal getSumPurchaseOrderAmount() {
        PurchaseOrderDO sumOrderAmount = purchaseOrderMapper.getSumOrderAmount();
        return sumOrderAmount.getTotalAmount();
    }

    @Override
    public BigDecimal getSumSaleOrderAmountBySyncPay(Integer syncPay) {
        SaleOrderDO saleOrderDO = saleOrderMapper.selectAmountTotalBySync(syncPay);
        return saleOrderDO.getTotalAmount();
    }

    @Override
    public BigDecimal getSumPurchaseOrderAmountBySyncPay(Integer syncPay) {
        PurchaseOrderDO sumSaleOrderAmount = purchaseOrderMapper.getSumOrderAmountBySyncPay(syncPay);
        return sumSaleOrderAmount.getTotalAmount();
    }
}
